//
//  ApplyLoanAuthController.swift
//  xiaoqiao
//
//  Created by 冷子豪 on 2017/7/13.
//  Copyright © 2017年 冷子豪. All rights reserved.
//

import UIKit

class ApplyLoanAuthController: UIViewController {

    // @IBOutlet weak var frontLabel: UILabel!
    @IBOutlet weak var frontImage: UIImageView!
    
    @IBOutlet weak var backLabel: UILabel!
    @IBOutlet weak var backImage: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        frontImage.isUserInteractionEnabled = true
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func nextClick(_ sender: UIButton) {
        if true {
            // 跳转界面 到 房产信息
            performSegue(withIdentifier: "auth2house", sender: nil)
        }
    }
}

//MARK: 轻触 图片控件
extension ApplyLoanAuthController {
    
    // MARK: 用于弹出选择的对话框界面
    var selectorController: UIAlertController {
        let controller = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        controller.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil)) // 取消按钮
        controller.addAction(UIAlertAction(title: "拍照选择", style: .default) { action in
            self.selectorSourceType(type: .camera)
        }) // 拍照选择
        controller.addAction(UIAlertAction(title: "相册选择", style: .default) { action in
            self.selectorSourceType(type: .photoLibrary)
        }) // 相册选择
        return controller
    }
    
    // MARK: 轻触手势事件的回调
    @IBAction func onTapImageView(sender: UITapGestureRecognizer) {
        print("!!!")
        present(selectorController, animated: true, completion: nil)
    }
    
    func selectorSourceType(type: UIImagePickerControllerSourceType) {
        imagePickerController.sourceType = type
        // 打开图片选择器
        present(imagePickerController, animated: true, completion: nil)
    }
}

//MARK: 扩展图片选择和结果返回
extension ApplyLoanAuthController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    // MARK: 图片选择器界面
    var imagePickerController: UIImagePickerController {
        get {
            let imagePicket = UIImagePickerController()
            imagePicket.delegate = self
            imagePicket.sourceType = .photoLibrary
            return imagePicket
        }
    }
    
    // MARK: 当图片选择器选择了一张图片之后回调
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: AnyObject]) {
        dismiss(animated: true, completion: nil) // 选中图片, 关闭选择器...这里你也可以 picker.dismissViewControllerAnimated 这样调用...但是效果都是一样的...
        
        frontImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage // 显示图片
        frontImage.contentMode = .scaleToFill // 缩放显示, 便于查看全部的图片
    }
    
    // MARK: 当点击图片选择器中的取消按钮时回调
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil) // 效果一样的...
    }
}
